﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApMlmPlacementManipulator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApMlmPlacementManipulator
{
  private TxPlacementManipulator _placementManipulator;
  private Action _elementPicked;
  private Action<TxTransformation> _actionOnManipulatorMovement;

  public CApMlmPlacementManipulator(Action placementManipulator_ElementPicked)
  {
    this._placementManipulator = new TxPlacementManipulator();
    this._elementPicked = placementManipulator_ElementPicked;
  }

  public void Activate(
    TxPlacementManipulatorParameters placementParams,
    Action<TxTransformation> actionOnManipulatorMovement)
  {
    this.RegisterPlacementManipulatorEvents();
    this._placementManipulator.Show(placementParams);
    this._actionOnManipulatorMovement = actionOnManipulatorMovement;
  }

  public void Deactivate()
  {
    this.UnRegisterPlacementManipulatorEvents();
    this._placementManipulator.Hide();
    this._actionOnManipulatorMovement = (Action<TxTransformation>) null;
  }

  public void RefreshReferenceFrame(string name, TxTransformation location)
  {
    if (string.IsNullOrEmpty(name) || !TxTransformation.op_Inequality(location, (TxTransformation) null) || !this.IsActive())
      return;
    this._placementManipulator.AddReferenceFrame(name, location);
    this._placementManipulator.SelectReferenceFrame(name);
  }

  public bool IsActive()
  {
    return this._placementManipulator != null && this._placementManipulator.IsVisible;
  }

  public TxPlacementManipulatorElement SelectedAxis
  {
    get => this._placementManipulator.SelectedElement;
    set
    {
      if (!this.IsActive() || this._placementManipulator.SelectedElement == value)
        return;
      this._placementManipulator.SelectedElement = value;
    }
  }

  public bool IsPlaneSelected
  {
    get
    {
      if (!this.IsActive())
        return false;
      return this._placementManipulator.SelectedElement == 10 || this._placementManipulator.SelectedElement == 14 || this._placementManipulator.SelectedElement == 12;
    }
  }

  private void RegisterPlacementManipulatorEvents()
  {
    this._placementManipulator.ManipulatorMoved += new TxPlacementManipulator_ManipulatorMovedEventHandler(this._placementManipulator_ManipulatorMoved);
    this._placementManipulator.ElementPicked += new TxPlacementManipulator_ElementPickedChangedEventHandler(this._placementManipulator_ElementPicked);
  }

  private void _placementManipulator_ElementPicked(
    object sender,
    TxPlacementManipulator_ElementPickedEventArgs args)
  {
    this._elementPicked();
  }

  private void UnRegisterPlacementManipulatorEvents()
  {
    this._placementManipulator.ManipulatorMoved -= new TxPlacementManipulator_ManipulatorMovedEventHandler(this._placementManipulator_ManipulatorMoved);
    this._placementManipulator.ElementPicked -= new TxPlacementManipulator_ElementPickedChangedEventHandler(this._placementManipulator_ElementPicked);
  }

  private void _placementManipulator_ManipulatorMoved(
    object sender,
    TxPlacementManipulator_ManipulatorMovedEventArgs args)
  {
    Action<TxTransformation> manipulatorMovement = this._actionOnManipulatorMovement;
    if (manipulatorMovement == null)
      return;
    manipulatorMovement(args.DeltaTransformation);
  }
}
